<script>
	// H5所需sdk  不用就删掉   package.json也删掉，不然会影响小程序的打包大小
	// 静默更新
	import silentlyAppUpdate from '@/uni_modules/wu-app-update/js-sdk/silentlyAppUpdate.js';
	import {
		validateNull,pushMessageReceiveClick
	} from '@/common/index.js'
	// #ifdef MP-WEIXIN  || APP-PLUS || H5
	import TIM from 'tim-wx-sdk/tim-wx-friendship.js';
	import COS from 'cos-wx-sdk-v5';
	// #endif

	let sdkAppID = 1400816272;

	// APP通话插件
	// #ifdef  APP-PLUS
	// const TUICallKit = uni.requireNativePlugin('TencentCloud-TUICallKit');
	// TUICallKit.enableFloatWindow(true);
	// #endif
	import Vue from 'vue';
	export default {
		async onLaunch() {
			Vue.prototype.TIM = TIM;
			this.initialization();
			// #ifdef APP-PLUS
			this.getClientId()
			silentlyAppUpdate();
			await this.getSystemInfo()
			//显示本地通知
			pushMessageReceiveClick()
			// #endif
		},
		async onShow() {
			let that = this
			// uni.login({
			//   provider: 'weixin',
			//   success: (code) => {
			//     that.post('api/client/code', {
			//       code: code.code
			//     }, true).then(res => {
			//     })
			//   }
			// })
			// #ifdef APP-PLUS || H5
			// uni.navigateTo({
			//   url: '/personal/otherPhoneLogin/index'
			// });
			// #endif
			uni.setStorageSync('tan_type', '0')
			this.initialization();
			uni.removeStorageSync('sheng')
			uni.removeStorageSync('shi')
			// if (uni.getStorageSync('token')) {
			// 	this.post('api/client/info', '', false).then(res => {
			// 		if (res.code == 401) {
			// 			// uni.showToast({
			// 			//   title: '登录失效，请重新登录',
			// 			//   icon: 'none'
			// 			// });
			// 			uni.removeStorageSync('token')
			// 		} else {}
			// 	})
			// }
		},
		onHide() {
			uni.setStorageSync('tan_type', '0')
		},
		globalData: {
			userInfo: null,
			userProfile: null,
			headerHeight: 0,
			statusBarHeight: 0,
		},
		methods: {
			getClientId() {
				let getCidTime = 120
				// 获取客户端标识
				let pinf = plus.push.getClientInfo();
				if (!validateNull(pinf) && !validateNull(pinf.clientid)) {
					uni.setStorageSync('clientid', pinf.clientid);
				} else {
					function getCid() {
						getCidTime--
						var obtainingCIDTimer = setTimeout(function() {
							pinf = plus.push.getClientInfo();
							if (!validateNull(pinf) && !validateNull(pinf.clientid)) {
								uni.setStorageSync('clientid', pinf.clientid);
								clearTimeout(obtainingCIDTimer);
							} else {
								if (getCidTime >= 0) {
									getCid()
								} else {}
							}
						}, 50);
					}
					getCid()
				}
			},
			async getSystemInfo() {
				let data = await uni.getSystemInfoSync()
				uni.setStorageSync('systemInfo', data)
			},
			// 初始化 im
			initialization() {
				const sdkID = uni.getStorageSync('sdkAppID');
				if (sdkID) {
					sdkAppID = sdkID;
				}

				// 初始化tim
				uni.$TUIKit = TIM.create({
					SDKAppID: sdkAppID
				});
				wx.$TUIKit = uni.$TUIKit;

				// #ifdef  H5
				uni.$TUIKit.registerPlugin({
					'cos-js-sdk': COS
				});
				// #endif

				// #ifdef  APP-PLUS || MP-WEIXIN
				uni.$TUIKit.registerPlugin({
					'cos-wx-sdk': COS
				});
				// #endif

				// #ifdef  APP-PLUS
				// uni.$TUICallKit = TUICallKit;
				// #endif

				uni.$TUIKit.setLogLevel(3);

				uni.$TUIKitTIM = TIM;
				uni.$TUIKitEvent = TIM.EVENT;
				uni.$TUIKitVersion = TIM.VERSION;
				uni.$TUIKitTypes = TIM.TYPES; // 监听系统级事件
				uni.$resetLoginData = this.resetLoginData();

				uni.$TUIKit.on(uni.$TUIKitEvent.SDK_NOT_READY, this.onSdkNotReady);
				uni.$TUIKit.on(uni.$TUIKitEvent.KICKED_OUT, this.onKickedOut);
				uni.$TUIKit.on(uni.$TUIKitEvent.ERROR, this.onTIMError);
				uni.$TUIKit.on(uni.$TUIKitEvent.NET_STATE_CHANGE, this.onNetStateChange);
				uni.$TUIKit.on(uni.$TUIKitEvent.SDK_RELOAD, this.onSDKReload);
				uni.$TUIKit.on(uni.$TUIKitEvent.SDK_READY, this.onSDKReady);

				// 是否登录
				this.isLogin();
			},
			isLogin() {
				let token = uni.getStorageSync('token')
				if (!token) return
				const userID = uni.getStorageSync('userID');
				const userSig = uni.getStorageSync('userSig');
				if (userID) {
					// 登录im
					uni.$TUIKit
						.login({
							userID: userID,
							userSig: userSig
						})
						.then(res => {
							// this.$u.toast('首次进来im登录成功')
						})
						.catch(() => {});
					// 登录 native 原生通话插件
					// #ifdef  APP-PLUS
					// uni.$TUICallKit.login({
					// 		SDKAppID: sdkAppID,
					// 		userID: userID,
					// 		userSig: userSig
					// 	},
					// 	res => {
					// 		if (res.code === 0) {
					// 		} else {
					// 		}
					// 	}
					// );
					// #endif
				}
			},
			// TODO:
			resetLoginData() {
				this.globalData.expiresIn = '';
				this.globalData.sessionID = '';
				this.globalData.userInfo = {
					userID: '',
					userSig: '',
					token: '',
					phone: ''
				};
				this.globalData.userProfile = null;
			},
			onTIMError() {},
			onNetStateChange() {},
			onSDKReload() {},
			onSDKReady() {
				this.$store.commit('login');
			},
			onSdkNotReady() {},
			onKickedOut() {
				// setTimeout(() => {
				//   uni.showToast({
				//     title: '您被踢下线',
				//     icon: 'error'
				//   });
				// }, 1000);
				// uni.navigateTo({
				//   url: '/im/login/login'
				// });
			}
		}
	};
</script>
<style lang="scss">
	/*每个页面公共css */
	@import "uview-ui/index.scss";
	@import "./common/iconfont.css";

	@font-face {
		font-family: "Demibold";
		src: url("https://static-1310167831.cos.ap-nanjing.myqcloud.com/uploads/20230527/74677b062210a34a6a67c59e03cc7f11.ttf") format("truetype");
		// src: url('https://wechat-1313723534.cos.ap-shanghai.myqcloud.com/uploads/20220905/87bf0e05746758faeda5709c962e5768.ttf') format('truetype');
		// src: url('https://wechat-1313723534.cos.ap-shanghai.myqcloud.com/uploads/20220905/87bf0e05746758faeda5709c962e5768.ttf') format('truetype');
	}

	@font-face {
		font-family: "Medium";

		src: url("https://wechat-1313723534.cos.ap-shanghai.myqcloud.com/uploads/20220905/87bf0e05746758faeda5709c962e5768.ttf") format("truetype");
	}

	@font-face {
		font-family: "Semibold";

		src: url("https://wechat-1313723534.cos.ap-shanghai.myqcloud.com/uploads/20220905/1cd072309d834e4b9d0a409789652724.ttf") format("truetype");
	}

	view {
		font-family: PingFang SC-Regular !important;
	}

	.yihang {
		white-space: nowrap;
		text-overflow: ellipsis;
		overflow: hidden;
		word-break: break-all;
	}

	.erhang {
		text-overflow: -o-ellipsis-lastline;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		line-clamp: 2;
		-webkit-box-orient: vertical;
	}

	.sanhang {
		text-overflow: -o-ellipsis-lastline;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 3;
		line-clamp: 3;
		-webkit-box-orient: vertical;
	}

	.sihang {
		text-overflow: -o-ellipsis-lastline;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 4;
		line-clamp: 4;
		-webkit-box-orient: vertical;
	}

	.wuhang {
		text-overflow: -o-ellipsis-lastline;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-line-clamp: 5;
		line-clamp: 5;
		-webkit-box-orient: vertical;
	}

	.flex {
		display: flex;
	}

	.items-center {
		display: flex;
		align-items: center;
	}

	.justify-center {
		display: flex;
		justify-content: center;
	}

	.items-justify-center {
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.justify-between {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.justify-between1 {
		display: flex;
		justify-content: space-between;
	}

	.flex-column {
		flex-direction: column;
	}

	.flex-end {
		display: flex;
		justify-content: flex-end;
	}

	.flex-row {
		flex-direction: row;
	}

	.flex-wrap {
		flex-wrap: wrap;
	}

	.flex-1 {
		flex: 1;
	}

	.prow76 {
		padding-left: 76rpx;
		padding-right: 76rpx;
	}

	.prow30 {
		padding-left: 30rpx;
		padding-right: 30rpx;
	}

	.prow20 {
		padding-left: 20rpx;
		padding-right: 20rpx;
	}

	.prow10 {
		padding-left: 10rpx;
		padding-right: 10rpx;
	}

	.text-white {
		color: #fff;
	}

	.text_theme {
		color: #18a9a5;
	}

	.text-000 {
		color: #000;
	}

	.text-333 {
		color: #333;
	}

	.text-666 {
		color: #666;
	}

	.text-999 {
		color: #999;
	}

	.text-black {
		color: #262d2c;
	}

	.tal {
		text-align: left;
	}

	.tac {
		text-align: center;
	}

	.tar {
		text-align: right;
	}

	.bold {
		font-weight: bold;
	}

	.fs40 {
		font-size: 40rpx;
	}

	.fs36 {
		font-size: 36rpx;
	}

	.fs34 {
		font-size: 34rpx;
	}

	.fs32 {
		font-size: 32rpx;
	}

	.fs30 {
		font-size: 30rpx;
	}

	.fs28 {
		font-size: 28rpx;
	}

	.fs26 {
		font-size: 26rpx;
	}

	.fs24 {
		font-size: 24rpx;
	}

	.fs22 {
		font-size: 22rpx;
	}

	.mt4 {
		margin-top: 4rpx;
	}

	.mt6 {
		margin-top: 6rpx;
	}

	.mt8 {
		margin-top: 8rpx;
	}

	.mt10 {
		margin-top: 10rpx;
	}

	.mt12 {
		margin-top: 12rpx;
	}

	.mt16 {
		margin-top: 16rpx;
	}

	.mt20 {
		margin-top: 20rpx;
	}

	.mt30 {
		margin-top: 30rpx;
	}

	.mt40 {
		margin-top: 40rpx;
	}

	.mb4 {
		margin-bottom: 4rpx;
	}

	.mb6 {
		margin-bottom: 6rpx;
	}

	.mb8 {
		margin-bottom: 8rpx;
	}

	.mb10 {
		margin-bottom: 10rpx;
	}

	.mb12 {
		margin-bottom: 12rpx;
	}

	.mb16 {
		margin-bottom: 16rpx;
	}

	.mb20 {
		margin-bottom: 20rpx;
	}

	.mb30 {
		margin-bottom: 30rpx;
	}

	.mb40 {
		margin-bottom: 40rpx;
	}

	.ml4 {
		margin-left: 4rpx;
	}

	.ml6 {
		margin-left: 6rpx;
	}

	.ml8 {
		margin-left: 8rpx;
	}

	.ml10 {
		margin-left: 10rpx;
	}

	.ml12 {
		margin-left: 12rpx;
	}

	.ml16 {
		margin-left: 16rpx;
	}

	.ml20 {
		margin-left: 20rpx;
	}

	.ml30 {
		margin-left: 30rpx;
	}

	.ml40 {
		margin-left: 40rpx;
	}

	.mr4 {
		margin-left: 4rpx;
	}

	.mr6 {
		margin-right: 6rpx;
	}

	.mr8 {
		margin-right: 8rpx;
	}

	.mr10 {
		margin-right: 10rpx;
	}

	.mr12 {
		margin-right: 12rpx;
	}

	.mr16 {
		margin-right: 16rpx;
	}

	.mr20 {
		margin-right: 20rpx;
	}

	.mr30 {
		margin-right: 30rpx;
	}

	.mr40 {
		margin-right: 40rpx;
	}

	.pl4 {
		padding-left: 4rpx;
	}

	.pl6 {
		padding-left: 6rpx;
	}

	.pl8 {
		padding-left: 8rpx;
	}

	.pl10 {
		padding-left: 10rpx;
	}

	.pl12 {
		padding-left: 12rpx;
	}

	.pl16 {
		padding-left: 16rpx;
	}

	.pl20 {
		padding-left: 20rpx;
	}

	.pl30 {
		padding-left: 30rpx;
	}

	.pr4 {
		padding-right: 4rpx;
	}

	.pr6 {
		padding-right: 6rpx;
	}

	.pr8 {
		padding-right: 8rpx;
	}

	.pr10 {
		padding-right: 10rpx;
	}

	.pr12 {
		padding-right: 12rpx;
	}

	.pr16 {
		padding-right: 16rpx;
	}

	.pr20 {
		padding-right: 20rpx;
	}

	.pr26 {
		padding-right: 26rpx;
	}

	.pr30 {
		padding-right: 30rpx;
	}

	.pr40 {
		padding-right: 40rpx;
	}

	.pt10 {
		padding-top: 10rpx;
	}

	.pt16 {
		padding-top: 16rpx;
	}

	.pt20 {
		padding-top: 20rpx;
	}

	.pt24 {
		padding-top: 24rpx;
	}

	.pt30 {
		padding-top: 30rpx;
	}

	.pt36 {
		padding-top: 36rpx;
	}

	.pt40 {
		padding-top: 40rpx;
	}

	.pb10 {
		padding-bottom: 10rpx;
	}

	.pb16 {
		padding-bottom: 16rpx;
	}

	.pb20 {
		padding-bottom: 20rpx;
	}

	.pb24 {
		padding-bottom: 24rpx;
	}

	.pb30 {
		padding-bottom: 30rpx;
	}

	.pb32 {
		padding-bottom: 32rpx;
	}

	.pb36 {
		padding-bottom: 36rpx;
	}

	.pb40 {
		padding-bottom: 40rpx;
	}

	.p-30 {
		padding: 30rpx;
	}

	.p-36 {
		padding: 36rpx;
	}

	.p-40 {
		padding: 40rpx;
	}

	.w-30 {
		width: 30rpx;
		height: 30rpx;
	}

	.w-32 {
		width: 32rpx;
		height: 32rpx;
	}

	.w-36 {
		width: 36rpx;
		height: 36rpx;
	}

	.w-40 {
		width: 40rpx;
		height: 40rpx;
	}

	.w-44 {
		width: 44rpx;
		height: 44rpx;
	}

	.w-48 {
		width: 48rpx;
		height: 48rpx;
	}

	.w-50 {
		width: 50rpx;
		height: 50rpx;
	}

	.w-56 {
		width: 56rpx;
		height: 56rpx;
	}

	.w-60 {
		width: 60rpx;
		height: 60rpx;
	}

	.w-64 {
		width: 64rpx;
		height: 64rpx;
	}

	.w-68 {
		width: 68rpx;
		height: 68rpx;
	}

	.w-72 {
		width: 72rpx;
		height: 72rpx;
	}

	.w-76 {
		width: 76rpx;
		height: 76rpx;
	}

	.w-80 {
		width: 80rpx;
		height: 80rpx;
	}

	.w-full {
		width: 100%;
	}

	.h-full {
		height: 100%;
	}

	.w-full-screen {
		width: 100vw;
	}

	.h-full-screen {
		height: 100vh;
	}

	.relative {
		position: relative;
		z-index: 0;
	}

	.relative_1 {
		position: relative;
		z-index: 1;
	}

	.bg-white {
		background-color: #fff;
	}

	.rounded-4 {
		border-radius: 4rpx;
	}

	.rounded-8 {
		border-radius: 8rpx;
	}

	.rounded-16 {
		border-radius: 16rpx;
	}

	.rounded-20 {
		border-radius: 20rpx;
	}

	.w-120 {
		width: 120rpx;
	}

	.label_name {
		color: #18a9a5;
		font-size: 20rpx;
		padding: 0px 10rpx;
		background: #81d8d0;
		border-radius: 14rpx;
		display: inline-block;
		height: 30rpx;
		line-height: 30rpx;
	}

	.bb_gray_2 {
		border-bottom: 1px solid #ececec;
	}

	.break_all {
		word-break: break-all;
	}
</style>